﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class CalendarGroupDetailBLL : IBaseInfoDetail
    {
        private static readonly ICalendarGroupDetailDA calendarGroupDetailDA = DataAccess.CreateCalendarGroupDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRateDA rateDA = DataAccess.CreateRateDA();

        public CalendarGroupDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CalendarGroupDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> calendarGroupID, List<string> LineID, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<CalendarGroupDetailInfo> CalendarGroupDetailInfos = new List<CalendarGroupDetailInfo>();
                CalendarGroupDetailInfo calendarGroupDetailInfo = null;
                foreach (string id in LineID)
                {
                    calendarGroupDetailInfo = calendarGroupDetailDA.GetCalendarGroupDetailByID(dataBase, tran, calendarGroupID[0], id, this.dataBaseID[0]);
                    CalendarGroupDetailInfos.Add(calendarGroupDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (calendarGroupDetailDA.DeleteCalendarGroupDetail(dataBase, tran, calendarGroupID[0], LineID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < LineID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CalendarGroupDetailInfo>(dataBase, tran, CalendarGroupDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                        string CS00003 = temp;
                        temp = CS00003 + calendarGroupID[0] + "," + LineID[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            throw new NotImplementedException();
        }

        public DataSet GetDetaiInfolByQueryEntity(object detailQuery)
        {
            throw new NotImplementedException();
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            throw new NotImplementedException();
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException();
        }

        public string InsertDetail(object obj)
        {
            CalendarGroupDetailInfo calendarGroupDetailInfo = obj as CalendarGroupDetailInfo;
            string lineID = string.Empty;
            try
            {
                if (calendarGroupDetailInfo == null)
                {
                    return lineID;
                }
                if (!rateDA.CheckRateIDUnique(calendarGroupDetailInfo.RateID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9190_001", this.pubParasInfo.UserLanguage, new List<string> { calendarGroupDetailInfo.RateID });
                }
                calendarGroupDetailInfo.UpdatedBy = calendarGroupDetailInfo.CreatedBy;
                if (calendarGroupDetailDA.InsertCalendarGroupDetail(calendarGroupDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CalendarGroupDetailInfo>(null, calendarGroupDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "CALENDAR_GROUP_DETAIL", this.dataBaseID[0]);
                    lineID = calendarGroupDetailInfo.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return lineID;
        }

        public string UpdateDetail(object obj)
        {
            throw new NotImplementedException();
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

